package com.yifan.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author YIFan
 * @Date 2020/7/15 7:57
 * @Version 1.0
 */
public class Test {

    public static void main(String[] args){



        try {
//            Class user = Class.forName("com.yifan.reflex.User");;
            Class user = Class.forName("com.yifan.reflex.User");

            //----- 1
            User user1 = new User();
            Class<? extends User> aClass = user1.getClass();
            //----- 2
            Class<User> u1 = User.class;

            System.out.println(u1 == aClass);//true'

            System.err.println(u1); // class com.yifan.reflex.User
            System.err.println(aClass); // class com.yifan.reflex.User


            // 获得构造对象
            Constructor<User> constructor = user.getConstructor(Integer.class, String.class);
            User hahah = constructor.newInstance(1, "hahah");
            System.out.println(hahah.toString());


            List<String> list = new ArrayList<>();
            list.add("2131");


            User o = (User) user.newInstance();
            System.out.println(o.toString());


        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

}


class User{
    private Integer age = 11;
    private String name = "jack";

    public User(){
        super();
    }

    public User(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}